package com.ipc.app.web.controller.passager;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.ipc.app.constants.AddressTypeConstants;
import com.ipc.app.constants.UserRoleConstants;
import com.ipc.app.constants.SessionConstants;
import com.ipc.app.exception.IPCBusinessException;
import com.ipc.app.model.Address;
import com.ipc.app.model.Information;
import com.ipc.app.service.InformationService;

/**
 * @author haijie.zhang
 * 
 */
@Controller
@RequestMapping(value = "/jsp/passager/InformationRelease.do")
public class InformationReleaseAction {

	@Autowired
	private InformationService informationService;

	@RequestMapping
	public String view() {
		return "passager/InformationRelease";
	}

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView informationRelease(ModelMap model,
			HttpServletRequest request) {

		Information information = getInformation(request);
		try {
			informationService.informationRelease(information);
		} catch (IPCBusinessException e) {
			// TODO:日志完善及异常捕获
			e.printStackTrace();
		}
		return new ModelAndView(new RedirectView("myReleaseInformation.do"));
	}

	private Address departureAddress(HttpServletRequest request) {
		Address address = new Address();
		address.setCity(request.getParameter("dep_city"));
		address.setDistrict(request.getParameter("dep_district"));
		address.setProvince(request.getParameter("dep_province"));
		address.setStreet(request.getParameter("dep_street"));
		address.setStreetNumber(request.getParameter("dep_streetNumber"));
		address.setX(Double.valueOf(request.getParameter("dep_x")));
		address.setY(Double.valueOf(request.getParameter("dep_y")));
		address.setType(AddressTypeConstants.DEPARTURE);
		address.setPointName(request.getParameter("dep_pointName"));
		return address;
	}

	private Address destinationAddress(HttpServletRequest request) {
		Address address = new Address();
		address.setCity(request.getParameter("des_city"));
		address.setDistrict(request.getParameter("des_district"));
		address.setProvince(request.getParameter("des_province"));
		address.setStreet(request.getParameter("des_street"));
		address.setStreetNumber(request.getParameter("des_streetNumber"));
		address.setX(Double.valueOf(request.getParameter("des_x")));
		address.setY(Double.valueOf(request.getParameter("des_y")));
		address.setType(AddressTypeConstants.DESTINATION);
		address.setPointName(request.getParameter("des_pointName"));
		return address;
	}

	private Information getInformation(HttpServletRequest request) {
		Information information = new Information();
		information.setMemo(request.getParameter("memo"));

		String userID = (String) request.getSession().getAttribute(
				SessionConstants.LOGIN_USER_ID);
		information.setUserId(userID);

		// TODO:价格转换
		information.setPrice(Integer.valueOf(request.getParameter("price")));
		information.setSeatNumber(Integer.valueOf(request
				.getParameter("bookSeatNumber")));

		information.setType(UserRoleConstants.PASSENGER);

		// TODO:日期控件完成之后更改
		information.setDepartureTime(new Date());

		Address departure = departureAddress(request);
		Address destination = destinationAddress(request);
		List<Address> addresses = new LinkedList<Address>();
		addresses.add(departure);
		addresses.add(destination);

		information.setAddresses(addresses);
		return information;

	}
}
